<template>
  <div>
    <!-- 商品列表代码 -->
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索功能，直接使用传进来的参数进行数据列表的重新获取 -->
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query" clearable @clear="getGoodsList"> <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button> </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="jumpAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="150px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="150px"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px">
          <!-- 使用插槽来使用时间组件 -->
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!-- 编辑按键 -->
            <el-button type="primary" icon="el-icon-edit"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" @click="removeGoodsById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 30, 50]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsList();
  },
  data() {
    return {
      // 商品列表数据
      goodsList: [],
      // 请求参数
      queryInfo: {
        // 查询值
        query: "",
        // 当前渲染页面,当前页码
        pagenum: 1,
        // 当前每次显示多少条数据
        pagesize: 10,
      },
      total: 0,
    };
  },
  methods: {
    // 获取整张表
    async getGoodsList() {
      // 传对象就是params
      const { data: res } = await this.$http.get("goods", { params: this.queryInfo });
      if (res.meta.status !== 200) {
        return this.$message.error("商品列表数据获取失败");
      } else {
        // 看接口数据在哪些位置
        this.goodsList = res.data.goods;
        // 获取总页数
        this.total = res.data.total;
        return this.$message.success("商品列表数据获取成功");
      }
    },
    // 监听pagesize改变事件
    handleSizeChange(newSize) {
      // 改变pagesize
      this.queryInfo.pagesize = newSize;
      // 刷新页面
      this.getGoodsList();
    },
    // 监听pagenum改变事件
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum;
      this.getGoodsList();
    },
    // 添加按键点击事件的实现
    async addGoods() {
      const { data: res } = await this.$http.post("goods/");
      if (res.meta.status !== 200) {
        return this.$message.error("商品列表数据获取失败");
      } else {
        // 刷新页面
        this.getGoodsList();
        return this.$message.success("商品列表数据获取成功");
      }
    },
    // 删除按钮点击事件的实现
    async removeGoodsById(id) {
      // 此处需要异步操作
      const confirmResult = await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除");
      } else {
        // 此处需要异步操作
        const { data: res } = await this.$http.delete(`goods/${id}`);
        if (res.meta.status !== 200) {
          return this.$message.error("商品列表数据获取失败");
        } else {
          // 刷新页面
          this.getGoodsList();
          return this.$message.success("商品列表数据获取成功");
        }
      }
    },
    // 添加商品按键点击跳转
    jumpAddGoodsPage() {
      // 页面跳转
      this.$router.push("goods/add");
    },
  },
};
</script>

<style lang="less" scoped></style>
